//
//  LoginViewController.swift
//  Exchange
//
//  Created by rui on 2018/4/24.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class LoginViewController: AccountBaseViewController,InputViewDelegate {

    @IBOutlet weak var resetBtn: UIButton!
    @IBOutlet weak var registerBtn: UIButton!
    @IBOutlet weak var loginBtn: UIButton!
    @IBOutlet weak var passwordInputView: InputView!
    @IBOutlet weak var accountInputView: InputView!
    @IBOutlet weak var titleL: UILabel!
    
    var viewModel = LoginViewModel()
    var isNeedPop = false
    
    override func viewDidLoad() {
        super.viewDidLoad()
        initUI()
        initData()
    }
    func initUI() {
        self.titleL.text = kText("登录")
        self.accountInputView.placeholder = kText("邮箱或者手机号")
        self.accountInputView.input.text = AppInfo.default.account
        self.accountInputView.delegate = self
        self.passwordInputView.placeholder = kText("密码")
        self.passwordInputView.delegate = self
        self.passwordInputView.input.isSecureTextEntry = true
        self.loginBtn.isEnabled = false
        self.loginBtn.setTitle(kText("登录"), for: .normal)
        self.resetBtn.setTitle(kText("忘记密码"), for: .normal)
        self.resetBtn.setTitleColor(kTextColor, for: .normal)
        self.registerBtn.setTitle(kText("注册账号"), for: .normal)
        self.registerBtn.setTitleColor(kTextColor, for: .normal)
    }
    
    func initData() {
        viewModel.response = {[weak self](state,isSuccess,errorMsg)in
            if state == ResponseState.mine_login {
                if isSuccess {
                   CYMBShow.showMessage(kText("登录成功"))
                    AppSecurityInfo.default.archiveSecurityInfo()
                    if (self?.isNeedPop)!  {
                    self?.navigationController?.popToRootViewController(animated: true)
                    }else {
                      self?.navigationController?.popViewController(animated: true)
                    }
                }else {
                    dPrint(errorMsg ?? "")
                    if let msg = errorMsg {
                        CYMBShow.showMessage(msg)
                    }
                }
            }
        }
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }


    // MARK: - event
    @IBAction func login(_ sender: Any) {
        dPrint("登陆")
        self.view.endEditing(true)
        let account = self.accountInputView.input.text!
        let password = self.passwordInputView.input.text!
        viewModel.login(account: account, password: password)
    }
    
    @IBAction func register(_ sender: UIButton) {
        let vc = MobileRegisterViewController()
        self.navigationController?.pushViewController(vc, animated: true)
    }
    @IBAction func reset(_ sender: UIButton) {
        let vc = MobileResetPasswordViewController()
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
    func inputViewTextDidChange(inputView: InputView) {
        if let accountCount = self.accountInputView.input.text?.count,let passwordCount = self.passwordInputView.input.text?.count, accountCount > 0,  passwordCount > 0 {
            self.loginBtn.isEnabled = true
        }else {
            self.loginBtn.isEnabled = false
        }
    }
    
    @objc func back() {
        if !AppInfo.default.isLogin {
            self.navigationController?.popToRootViewController(animated: true)
        }
        self.navigationController?.popViewController(animated: true)
    }
    
    override func customBackItem() -> UIBarButtonItem? {
        let item = UIBarButtonItem(image: #imageLiteral(resourceName: "nav_icon_back"), style: .plain, target: self, action: #selector(back))
        return item
    }
}

